﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace RenRen.Common {
    public static class Extensions {
        /// <summary>
        /// 检查字符串是否是bool值
        /// </summary>
        /// <param name="test">string</param>
        /// <returns>bool</returns>
        public static bool IsBoolean(this string test) {
            return test.Equals("false") || test.Equals("true");
        }
        /// <summary>
        /// 检测输入的字符串是否是json数组
        /// </summary>
        /// <param name="test">string</param>
        /// <returns>bool</returns>
        public static bool IsJSONArray(this string test) {
            return test.StartsWith("{") && !test.StartsWith("{*") || test.StartsWith("[");
        }
        ///<summary>
        ///</summary>
        ///<param name="enumeratedType"></param>
        ///<typeparam name="T"></typeparam>
        ///<returns></returns>
        ///<exception cref="ArgumentException"></exception>
        public static string GetDescription<T>(this T enumeratedType) {
            var description = enumeratedType.ToString();

            var enumType = typeof(T);
            // Can't use type constraints on value types, so have to do check like this
            if (enumType.BaseType != typeof(Enum))
                throw new ArgumentException("T must be of type System.Enum");

            var fieldInfo = enumeratedType.GetType().GetField(enumeratedType.ToString());

            if (fieldInfo != null) {
                var attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attributes != null && attributes.Length > 0) {
                    description = ((DescriptionAttribute)attributes[0]).Description;
                }
            }

            return description;
        }

        ///<summary>
        ///</summary>
        ///<param name="enums"></param>
        ///<typeparam name="T"></typeparam>
        ///<returns></returns>
        ///<exception cref="ArgumentException"></exception>
        public static string GetDescription<T>(this Collection<T> enums) {
            var sb = new StringBuilder();

            var enumType = typeof(T);

            // Can't use type constraints on value types, so have to do check like this
            if (enumType.BaseType != typeof(Enum))
                throw new ArgumentException("T must be of type System.Enum");

            foreach (var enumeratedType in enums) {
                sb.AppendLine(enumeratedType.GetDescription());
            }

            return sb.ToString();
        }

        /// <summary>
        /// 把任意数组转换成简单的字符串,逗号分隔
        /// </summary>
        /// <param name="items">The array to convert to a comma separated list.</param>
        /// <returns>comma separated string.</returns>
        public static string AsString<T>(this T[] items) {
            return string.Join(",", (from a in items select a.ToString()).ToArray());
        }

        /// <summary>
        /// 把任意集合转换成简单的字符串,逗号分隔
        /// </summary>
        /// <param name="items">The array to convert to a comma separated list.</param>
        /// <returns>comma separated string.</returns>
        public static string AsString<T>(this IList<T> items) {
            return string.Join(",", (from a in items select a.ToString()).ToArray());
        }
        /// <summary>
        /// 移除无效的xml字符
        /// </summary>
        public static string StripNonValidXMLCharacters(this string s) {
            StringBuilder _validXML = new StringBuilder(s.Length, s.Length);  
            char[] charArray = s.ToCharArray();

            if (string.IsNullOrEmpty(s)) return string.Empty;  

            for (int i = 0; i < charArray.Length; i++) {
                char current = charArray[i];
                if ((current == 0x9) ||
                (current == 0xA) ||
                (current == 0xD) ||
                ((current >= 0x20) && (current <= 0xD7FF)) ||
                ((current >= 0xE000) && (current <= 0xFFFD)))
                    _validXML.Append(current);
            }
            return _validXML.ToString();
        }
    }
}
